package com.jrx.anytxn.customer.controller;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.customer.dto.BaseApiReq;
import com.jrx.anytxn.customer.dto.BaseApiRes;
import com.jrx.anytxn.customer.dto.customer.*;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.customer.handel.ICustomerHander;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @ClassName CustomerInfoController
 * @Description: 客户信息api接口
 * @Author liy
 * @Date 2019/7/19
 * @Version V1.0
 **/
@Api(tags = "客户管理的api接口")
@RestController
@RequestMapping(value = "/api/customer/customerInfo")

public class CustomerInfoController {
    private static Logger logger = LoggerFactory.getLogger(CustomerInfoController.class);
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICustomerHander customerHander;

    @ApiOperation(value = "客户建档", notes ="客户信息建档，刷新")
    @PostMapping(value = "/customerInfoCreation")
    public TxnRespResult<BaseApiRes> customerInfoCreation(@Valid @RequestBody CustomerReq req) throws Exception {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        customerHander.customerInfoCreation(req);
        BaseApiRes res = BeanMapping.copy(req, BaseApiRes.class);
        result.getSuccess(res);
        return result;
    }


    @ApiOperation(value = "客户信息查询", notes = "根据渠道和userId查询客户，正常情况下，一条数据")
    @PostMapping(value = "/customerInfoQuery")
    public TxnRespResult<CustomerWholeInfoRes> customerInfoQuery(@Valid @RequestBody BaseApiReq req) throws TxnBizException {
        TxnRespResult<CustomerWholeInfoRes> result = new TxnRespResult<>();
        CustomerWholeInfoRes res = customerService.customerInfoQuery(req);
        result.getSuccess(res);
        return result;
    }


    @ApiOperation(value = "客户信息查询", notes = "客户基础信息查询")
    @PostMapping(value = "/customerBaseInfoQuery")
    public TxnRespResult<CustomerBaseInfoRes> customerBaseInfoQuery(@Valid @RequestBody BaseApiReq req) throws TxnBizException {
        TxnRespResult<CustomerBaseInfoRes> result = new TxnRespResult<>();
        CustomerBaseInfoRes res = customerService.customerBaseInfoQuery(req);
        result.getSuccess(res);
        return result;
    }


    @ApiOperation(value = "客户信息查询", notes = "根据渠道和核心客户编号查询客户，正常情况下，一条数据")
    @PostMapping(value = "/customerFirstLeverQuery")
    public TxnRespResult<CustomerWholeInfoRes> customerFirstLeverQuery(@Valid @RequestBody CustomerFirstLeverQueryReq req) throws TxnBizException {
        TxnRespResult<CustomerWholeInfoRes> result = new TxnRespResult<>();
        CustomerWholeInfoRes res = customerService.customerFirstLeverQuery(req);
        result.getSuccess(res);
        return result;
    }


    @ApiOperation(value = "修改客群", notes = "客群标识修改")
    @PostMapping(value = "/customerGroupInfoUpdate")
    public TxnRespResult<BaseApiRes> customerGroupInfoUpdate(@Valid @RequestBody ModifyCustomerGroupReq req) throws TxnBizException {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        customerService.customerGroupInfoUpdate(req);
        BaseApiRes res = BeanMapping.copy(req, BaseApiRes.class);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation(value = "客户注册", notes = "客户注册api")
    @PostMapping(value = "/customerRegister")
    public TxnRespResult<CustomerRegisterRes> customerRegister(@Valid @RequestBody CustomerRegisterReq req) throws Exception {
        TxnRespResult<CustomerRegisterRes> result = new TxnRespResult<>();
        CustomerRegisterRes customerRegisterRes=new CustomerRegisterRes();
        customerRegisterRes.setSeqNo(req.getSeqNo());
        customerRegisterRes.setUserId(req.getUserId());
        customerHander.customerRegister(req);
        result.getSuccess(customerRegisterRes);
        return result;
    }
    @ApiOperation(value = "客户信息补充", notes = "客户信息补充api")
    @PostMapping(value = "/customerSupply")
    public TxnRespResult<CustomerSupplyRes> customerSupply(@Valid @RequestBody CustomerSupplyReq req) throws Exception {
        TxnRespResult<CustomerSupplyRes> result = new TxnRespResult<>();
        CustomerSupplyRes csr=new CustomerSupplyRes();
        csr.setSeqNo(req.getSeqNo());
        csr.setUserId(req.getUserId());

        //客户信息补充逻辑
        customerHander.customerSupply(req);

        //获取并返回ecif编号
        CmCustomerFirstLevel ccfl = customerService.getCustomerList("", req.getIdNumber(), req.getTenantId()).get(0);
        csr.setEcifId(ccfl.getEcifId());
        result.getSuccess(csr);
        return result;
    }

    @ApiOperation(value = "修改客户手机号", notes = "修改客户手机号api")
    @PostMapping(value = "/modifyMobile")
    public TxnRespResult<BaseApiRes> modifyMobile(@Valid @RequestBody CustomeMobileModifyReq req) throws Exception {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        CustomerMobileModifyRes customerRegisterRes=new CustomerMobileModifyRes();
        customerRegisterRes.setSeqNo(req.getSeqNo());
        customerRegisterRes.setUserId(req.getUserId());
        customerHander.modifyMobile(req);
        result.getSuccess(customerRegisterRes);
        return result;
    }

    @ApiOperation(value = "用户注册保存营销数据", notes = "用户注册保存营销数据api")
    @PostMapping(value = "/customerRegisterMarketingRecord")
    public TxnRespResult<CustomerRegisterMarketingRecordRes> customerRegisterMarketingRecord(@Valid @RequestBody CustomeRegisterMarketingRecordReq req) throws Exception {
        TxnRespResult<CustomerRegisterMarketingRecordRes> result = new TxnRespResult<>();
        CustomerRegisterMarketingRecordRes customerRegisterMarketingRecordRes = new CustomerRegisterMarketingRecordRes();
        customerRegisterMarketingRecordRes.setSeqNo(req.getSeqNo());
        customerRegisterMarketingRecordRes.setUserId(req.getUserId());
        customerHander.saveRegMarketingRecord(req);
        result.getSuccess(customerRegisterMarketingRecordRes);
        return result;
    }

    @ApiOperation(value = "证件有效期修改", notes = "证件有效期修改api")
    @PostMapping(value = "/updateIdValidity")
    public TxnRespResult<BaseApiRes> updateIdValidity(@Valid @RequestBody IdValidityReq req) throws Exception {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        customerHander.updateIdValidity(req);
        BaseApiRes res = BeanMapping.copy(req, BaseApiRes.class);
        result.getSuccess(res);
        return result;
    }



    @ApiOperation(value = "验证身份证有效期", notes = "验证身份证有效期")
    @PostMapping(value = "/validCard")
    public TxnRespResult<CustomerWholeInfoRes> validCard(@Valid @RequestBody BaseApiReq req) throws TxnBizException {
        TxnRespResult<CustomerWholeInfoRes> result = new TxnRespResult<>();
        CustomerWholeInfoRes res = customerService.validCard(req);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation(value = "二级客户信息更新同步", notes = "二级客户信息更新同步")
    @PostMapping(value = "/secondLevelUpdate")
    public TxnRespResult<BaseApiRes> updateSecondLevel(@Valid @RequestBody CustSecondLevelModReq req) throws Exception {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        customerHander.updateSecondLevel(req);
        BaseApiRes res = BeanMapping.copy(req, BaseApiRes.class);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation(value = "注册ECIF", notes = "注册ECIF")
    @PostMapping(value = "/ecifRegister")
    public TxnRespResult<CustomerEcifRes> ecifRegister(@Valid @RequestBody CustomerEcifReq req) throws Exception {
        TxnRespResult<CustomerEcifRes> result = new TxnRespResult<>();
        CustomerEcifRes res = new CustomerEcifRes();
        customerHander.ecifRegister(req);
        //获取并返回ecif编号
        CmCustomerFirstLevel ccfl = customerService.getCustomerList("", req.getIdNumber(), req.getTenantId()).get(0);
        res.setEcifId(ccfl.getEcifId());
        result.getSuccess(res);
        return result;
    }

    /**
     * 客户额外信息补充修改
     * @param req
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "客户额外信息补充修改", notes = "客户额外信息补充修改")
    @PostMapping(value = "/extInfo")
    public TxnRespResult<BaseApiRes> extInfo(@Valid @RequestBody CusExtInfoReq req) throws Exception {
        TxnRespResult<BaseApiRes> result = new TxnRespResult<>();
        BaseApiRes res = new BaseApiRes();
        BeanUtils.copyProperties(req, res);
        customerService.extInfo(req);
        result.getSuccess(res);
        return result;
    }
}
